<?php
/**
 * Xml class for 360CPS
 *
 */
class Cps360_Xml
{
	private $allow_types = array('order' , 'check') ;
	
	/**
	 * change the data to xml format
	 * Enter description here ...
	 * @param mixed $records
	 */
	public function format_data($records , $query_type = 'order')
	{
		if(!in_array($query_type , $this->allow_types)){
			
		}
		//none record input 
		if(empty($records)){
			return $this->output_xml() ;
		}
		$order_str = '' ;
		switch ($query_type){
			case 'query' :
				$order_str = $this->query_format($records) ;
				break ;
			case 'check' :
				$order_str = $this->check_format($records) ; 
				break ;
		}
		return $this->output_xml($order_str);
	}
	
	/**
	 * for checking orders
	 * @param mixed 
	 */
	private function check_format($orders){
		$xml_str = "" ;
		foreach ($orders as $val){
			$xml_str .=
				'<order>
					<order_id>'.$val['order_id'].'</order_id>
					<order_time>'.$val['order_time'].'</order_time>
					<order_updtime>'.$val['order_updtime'].'</order_updtime>
					<status>'.$val['status'].'</status>
					<server_price>'.$val['server_price'].'</server_price>
					<coupon>'.$val['coupon'].'</coupon>
					<total_price>'.$val['total_price'].'</total_price>
					<total_comm>'.$val['total_comm'].'</total_comm>
					<commission>'.implode('|',$val['comm_product']).'|'.($val['comm_coupon']).'</commission>
				</order>';
		}
		return $xml_str ;
	}
	
	/**
	 * query interface using
	 * Enter description here ...
	 */
	private function query_format($orders){
		$xml_str = "" ;
		foreach ($orders as $val)
		{
			$xml_str .= 
				'<order>
					<bid>'.CPS360_Conf::CPS_BID.'</bid>
					<qid>'.$val['qid'].'</qid>
					<qihoo_id>'.$val['qihoo_id'].'</qihoo_id>
					<ext>'.$val['ext'].'</ext>
					<order_id>'.$val['order_id'].'</order_id>
					<order_time>'.$val['order_time'].'</order_time>
					<order_updtime>'.$val['order_updtime'].'</order_updtime>
					<status>'.$val['status'].'</status>
					<server_price>'.$val['server_price'].'</server_price>
					<coupon>'.$val['coupon'].'</coupon>
					<total_price>'.$val['total_price'].'</total_price>
					<total_comm>'.$val['total_comm'].'</total_comm>
					<commission>'.implode('|',$val['comm_product']).'|'.($val['comm_coupon']).'</commission>
					<p_info>'.implode('|',$val['p_info']).'</p_info>
				</order>' ;
		}
		return $xml_str ;
	}
	
	/**
	 * Output the xml 
	 * @param string $xml_orders
	 */
	private function output_xml($xml_orders = '')
	{
		$xml  = '<?xml version="1.0" encoding="utf-8"?><orders>' ;
		$xml .= $xml_orders."\n" ; 
		$xml .= '</orders>' ;
		return $xml ;
	}
}